//
//  NearTheShopViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/4.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "NearTheShopViewController.h"
#import "WashTheWardrobeNearView.h"
#import "NearTheShopView.h"
#import "AllShopsNearbyView.h"

@interface NearTheShopViewController ()
{
    float bgViewHeight;
    NSString *chooseString;
    BOOL AccordingToTheStore;  //一级菜单是否显示店铺
    BOOL AddView;   //添加视图
}
Strong UIView *headView;
Strong UILabel *titleLabel;
Strong WashTheWardrobeNearView *washTheWardrobeNearView;
Strong NSMutableArray *washTheWardrobeNearArray; /**<*/
Strong NSMutableArray *nearTheShopViewArray; /**<*/
Strong NearTheShopView *nearTheShopView;
Strong NSMutableArray *collectionDataArray; /**<*/
Strong UIView *secondView;
Strong AllShopsNearbyView *allShopsNearbyView;
Strong NSMutableArray *allShopsNearbyArray; /**<*/

@end

//网络
@implementation NearTheShopViewController (NetWorking)

/**
 附近洗衣柜
 */
- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"longitude":[UserModel sharedInstanced].longitude,
                                 @"latitude":[UserModel sharedInstanced].latitude};
    [[AFAppDotNetAPIClient sharedClient] POST:@"wardrobe/Mobile_Phone_Terminal/showNearbyLaundryCabinet.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            [self.washTheWardrobeNearArray removeAllObjects];
            [self.washTheWardrobeNearArray addObjectsFromArray:responseObject[@"obj"]];
            [self.view addSubview:self.washTheWardrobeNearView];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

//-----------------------附近店铺

/**
 显示一起菜单
 */
- (void)getShowLevelHeadingsAPP{
    [self showprogressHUD];
    
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Header_Management/showLevelHeadingsAPP.action" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            NSArray *array = responseObject[@"obj"];
            for (int i = 0; i < array.count; i ++) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:array[i]];
                if (i == 0) {
                    [dic setObject:mainColor forKey:@"textColor"];
                }else{
                    [dic setObject:textColor forKey:@"textColor"];
                }
                [self.collectionDataArray addObject:dic];
            }
            [self showTwoTitleByFirstLevelTitle:self.collectionDataArray[0][@"lm_id"]];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 根据一级菜单显示二级菜单
 
 @param lm_id lm_id description
 */
- (void)showTwoTitleByFirstLevelTitle:(NSString *)lm_id{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"lmId":lm_id};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Header_Management/showTwoTitleByFirstLevelTitle.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.allShopsNearbyArray removeAllObjects];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            [self.allShopsNearbyArray addObjectsFromArray:responseObject[@"obj"]];
            chooseString = self.allShopsNearbyArray[0][@"tlm_name"];
            self.allShopsNearbyView.chooseString = chooseString;
            self.allShopsNearbyView.dataArray = self.allShopsNearbyArray;
            if (AccordingToTheStore) {
                [self latitudeAndLongitudeClassificationViewShop:self.allShopsNearbyArray[0][@"tlm_id"]];
            }
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 附近店铺显示(通过二级分类ID和当前用户经纬度进行查看)
 */
- (void)latitudeAndLongitudeClassificationViewShop:(NSString *)tlm_id{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"tlm_id":tlm_id,
                                 @"s_longitude":[UserModel sharedInstanced].longitude,
                                 @"s_latitude":[UserModel sharedInstanced].latitude};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Store_Mobile/latitudeAndLongitudeClassificationViewShop.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.nearTheShopViewArray removeAllObjects];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            [self.nearTheShopViewArray addObjectsFromArray:responseObject[@"obj"]];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        self.nearTheShopView.dataArray = self.nearTheShopViewArray;
        if (!AccordingToTheStore) {
            [self.view addSubview:self.secondView];
            AccordingToTheStore = NO;
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation NearTheShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    AccordingToTheStore = NO;
    AddView = NO;
    [self getShowLevelHeadingsAPP];
    [self.view sd_addSubviews:@[self.headView]];
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"附近洗衣店",@"附近店铺",nil];
    
    //初始化UISegmentedControl
    
    //    [self.navigationController.view addSubview:segmentedControl];
    self.navigationItem.titleView = ({
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
        segmentedControl.frame = CGRectMake(0, 0 , 200 , 30);
        // 设置默认选择项索引
        segmentedControl.selectedSegmentIndex = 0;
        [segmentedControl addTarget:self action:@selector(didClicksegmentedControlAction:) forControlEvents:UIControlEventValueChanged];
        segmentedControl.layer.cornerRadius = 15;
        segmentedControl;
    });
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - init

- (WashTheWardrobeNearView *)washTheWardrobeNearView{
    if (!_washTheWardrobeNearView) {
        _washTheWardrobeNearView = [[WashTheWardrobeNearView alloc]initWithFrame:CGRectMake(0, 32 / WIDTH_5S_SCALE, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 32 / WIDTH_5S_SCALE)];
        _washTheWardrobeNearView.dataArray = self.washTheWardrobeNearArray;
    }
    return _washTheWardrobeNearView;
}

- (UIView *)secondView{
    if (!_secondView) {
        _secondView = [[UIView alloc]initWithFrame:CGRectMake(0, 32 / WIDTH_5S_SCALE, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 32 / WIDTH_5S_SCALE)];
        self.allShopsNearbyView = nil;
        self.nearTheShopView = nil;
        [_secondView addSubview:self.nearTheShopView];
        [_secondView addSubview:self.allShopsNearbyView];
    }
    return _secondView;
}

- (NearTheShopView *)nearTheShopView{
    if (!_nearTheShopView) {
        _nearTheShopView = [[NearTheShopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 32 / WIDTH_5S_SCALE)];
        _nearTheShopView.bgViewHeight = self.allShopsNearbyView.height;
        _nearTheShopView.dataArray = self.nearTheShopViewArray;
        _nearTheShopView.collectionDataArray = self.collectionDataArray;
        __weak __typeof(self)weakSelf = self;
        _nearTheShopView.getCollViewCellIndexPath = ^(int a) {
            AccordingToTheStore = YES;
            [weakSelf showTwoTitleByFirstLevelTitle:weakSelf.collectionDataArray[a][@"lm_id"]];
        };
    }
    return _nearTheShopView;
}

- (AllShopsNearbyView *)allShopsNearbyView{
    if (!_allShopsNearbyView) {
        _allShopsNearbyView = [[AllShopsNearbyView alloc]initWithFrame:CGRectMake(0, 31 / WIDTH_5S_SCALE + 0.5, SCREEN_WIDTH, 38 / WIDTH_5S_SCALE * (self.allShopsNearbyArray.count / 4 + (self.allShopsNearbyArray.count % 4 == 0 ? 0 : 1))) selectedHandle:^(NSString * str) {
            chooseString = self.allShopsNearbyArray[[str intValue]][@"tlm_name"];
            _allShopsNearbyView.chooseString = chooseString;
            _allShopsNearbyView.dataArray = self.allShopsNearbyArray;
            [self latitudeAndLongitudeClassificationViewShop:self.allShopsNearbyArray[[str intValue]][@"tlm_id"]];
        }];
        _allShopsNearbyView.chooseString = chooseString;
        _allShopsNearbyView.dataArray = self.allShopsNearbyArray;
    }
    return _allShopsNearbyView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 32 / WIDTH_5S_SCALE)];
        _titleLabel.font = DEF_FontSize_15;
        _titleLabel.textColor = getColor(mainColor);
        _titleLabel.text = [NSString stringWithFormat:@"当前位置:%@",[UserModel sharedInstanced].locality];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 32 / WIDTH_5S_SCALE)];
        [_headView addSubview:self.titleLabel];
    }
    return _headView;
}

#pragma mark - click

-(void)didClicksegmentedControlAction:(UISegmentedControl *)Seg{
    
    NSInteger Index = Seg.selectedSegmentIndex;
    if (Index == 0) {
        [_secondView removeFromSuperview];
        [self getData];
    }else if (Index == 1){
        AccordingToTheStore = NO;
        [self.washTheWardrobeNearView removeFromSuperview];
        if (self.allShopsNearbyArray.count == 0) {
            ShowMessage(@"请返回之后再次进入");
            return;
        }
        [self latitudeAndLongitudeClassificationViewShop:self.allShopsNearbyArray[0][@"tlm_id"]];
    }
}


#pragma mark - data

/**
 二级菜单
 
 @return return value description
 */
- (NSMutableArray *)allShopsNearbyArray{
    if (!_allShopsNearbyArray) {
        _allShopsNearbyArray = @[].mutableCopy;
    }
    return _allShopsNearbyArray;
}

- (NSMutableArray *)washTheWardrobeNearArray{
    if (!_washTheWardrobeNearArray) {
        _washTheWardrobeNearArray  = @[
                                       ].mutableCopy;
    }
    return _washTheWardrobeNearArray;
}

- (NSMutableArray *)nearTheShopViewArray{
    if (!_nearTheShopViewArray) {
        _nearTheShopViewArray = @[].mutableCopy;
    }
    return _nearTheShopViewArray;
}

- (NSMutableArray *)collectionDataArray{
    if (!_collectionDataArray) {
        _collectionDataArray = @[].mutableCopy;
    }
    return _collectionDataArray;
}

@end
